package com.hindua.tab.scr;

import java.io.InputStream;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.ProgressBar;
import com.google.ads.AdRequest;
import com.google.ads.AdView;
import com.hindua.adapter.ItemAdapter;
import com.hindua.ailaaicuaai.ChapterActivity;
import com.hindua.ailaaicuaai.MainActivity;
import com.hindua.beans.Item;
import com.rtst.widget.tab.TabFragment;
import com.hindua.ailaaicuaai.R;

public class DangDocSrc extends TabFragment{
	private ViewGroup viewGroup;
	private ListView listView;
	private AdView adView;
	private ArrayList<Item> items = new ArrayList<Item>();
	private ItemAdapter adapter;
	private int positionBookmark;
	private String xmlFile = "list_item.xml";
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if(viewGroup == null) {
			viewGroup = (ViewGroup) inflater.inflate(R.layout.category_scr_layout, container, false);
			listView = (ListView) viewGroup.findViewById(android.R.id.list);
			adView = (AdView) viewGroup.findViewById(R.id.adView);
			AdRequest adRequest = new AdRequest();
	        adView.loadAd(adRequest);
	        setUp();
		} else {
			((ViewGroup) viewGroup.getParent()).removeView(viewGroup);
		}
		return viewGroup;
	}
	
	private void setUp() {
		parseXML();
		this.positionBookmark = MainActivity.getInstance().getPositionChapterBookmark();
		if (this.positionBookmark >= 0 && this.positionBookmark < this.items.size()) {
			ArrayList<Item> tmps = new ArrayList<Item>(); 
			tmps.add(this.items.get(this.positionBookmark));
			adapter = new ItemAdapter(getActivity(), tmps);
			listView.setAdapter(adapter);
			listView.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					Intent intent = new Intent(getActivity(), ChapterActivity.class);
					intent.putExtra("position", getPositionBookmark());
					intent.putExtra("items", getItems());
					startActivity(intent);
				}
			});
		} else {
			ArrayList<Item> tmps = new ArrayList<Item>();
			Item item = new Item();
			item.setId("-1");
			item.setName("Chưa có chương nào được lưu");
			tmps.add(item);
			adapter = new ItemAdapter(getActivity(), tmps);
			listView.setAdapter(adapter);
		}
	}
	
	private void parseXML() {
		try {
			InputStream in = getActivity().getAssets().open(xmlFile);
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(in);
			doc.getDocumentElement().normalize();
			NodeList nList = doc.getElementsByTagName("item");
			for (int i = 0; i < nList.getLength(); i ++) {
				Node node = nList.item(i);
				Item item = new Item();
				for (int j = 0; j < node.getChildNodes().getLength(); j ++) {
					Node tmp = node.getChildNodes().item(j);
					if (tmp.getNodeName().equalsIgnoreCase("id")) {
						item.setId(tmp.getTextContent());
						//System.out.println("--- ID: " + tmp.getTextContent());
					} else if (tmp.getNodeName().equalsIgnoreCase("name")) {
						item.setName(tmp.getTextContent());
						//System.out.println("--- NAME: " + tmp.getTextContent());
					}
				}
				this.items.add(item);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public ArrayList<Item> getItems() {
		return items;
	}

	public int getPositionBookmark() {
		return positionBookmark;
	}
}
